package com.example.online_order.service.impl;

import com.example.online_order.Eception.BussinessException;
import com.example.online_order.Eception.ExceptionType;
import com.example.online_order.mapper.GoodsMapper;
import com.example.online_order.pojo.Goods;
import com.example.online_order.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper mapper;

    @Transactional
    @Override
    public List<Goods> selectAllGoods() {
        List<Goods> goodsDb = mapper.getAllGoods();
        if ( goodsDb != null)
            return goodsDb;
        else{
            throw new BussinessException(ExceptionType.bus_id_not_found,"未找到商品");
        }
    }



    @Transactional
    @Override
    public Integer addGood(Goods goods) {
        Integer res = mapper.addGoods(goods);
        if (res!= 0)
            return res;
        else{
            throw new BussinessException(ExceptionType.bus_add_fail,"添加商品失败");
        }
    }

    @Transactional
    @Override
    public Integer deleteGood(Integer id) {
        Goods goodsDb = mapper.getById(id);
        if ( goodsDb ==null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"未找到该商品");
        }else{
            return mapper.deleteGoods(id);
        }

    }

    @Transactional
    @Override
    public Integer updateGood(Goods goods) {
        Goods goodsDb = mapper.getById(goods.getId());
        System.out.println(goodsDb);
        if ( goodsDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"未找到该商品");
        }else{
            Integer res = mapper.updateGoods(goods);
            if ( res != 1)
            {
                throw new BussinessException(ExceptionType.bus_update_fail,"修改商品失败");
            }else{
                return res;
            }
        }
    }

    @Override
    public List<Goods> selectDsql(String mealName, String mealType) {
        List<Goods> goodsDb = mapper.selectDsql(mealName, mealType);
        if ( goodsDb!= null)
        {
            return goodsDb;
        }else{
            throw new BussinessException(ExceptionType.bus_id_not_found,"未找到该类别的商品");
        }
    }

    @Override
    public List<Goods> selectMealType() {
        List<Goods> goodsDb = mapper.getmealType();
        if ( goodsDb!= null)
        {
            return goodsDb;
        }else{
            throw new BussinessException(ExceptionType.bus_id_not_found,"未找到该类别的商品");
        }
    }
}
